#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUF_SIZE 1024

typedef struct var_storage_s
{
    int var_type;
    char *var;
    void *val;
} var_t;

char *prog;
char buf[BUF_SIZE];

var_t *storage;

void run();

int main(int argc, char **argv)
{
    long flen;
    argv++;
    if(argc != 2)
        return 1;// printHelp();
    FILE *fprog = fopen(*argv, "r");
    if(fprog)
    {
        fseek(fprog, 0, SEEK_END);
        flen = ftell(fprog);
        prog = (char*)malloc(flen + 1);
        rewind(fprog);
        if(prog)
        {
            fread(prog, flen, 1, fprog);
            fclose(fprog);
            prog[flen] = 0;
            run();
        }
    }
    else
        return 1;
    return 0;
}

void run()
{
    int count, type;
    var_t *storage_ptr;
    char *entry;
    char *tmp1;
    int *tmp2;
    // Begin of init_stack
    entry = strstr(prog, "\ndef_count ");
    if(entry)
    {
        entry += 11;
        for(char *p1 = strchr(entry, '\n') - 1; ; p1--)
            if(*p1 != ' ')
            {
                p1++;
                *p1 = 0;
                count = atoi(entry) + 1;
                *p1 = ' ';
                break;
            }
        storage_ptr = storage = (var_t *)malloc(count * sizeof(var_t));
        storage_ptr[count - 1].var_type = 0;
        printf("%d个变量已入栈\n", count - 1);
    } else
        count = 0;
    for(int i = 0; count; i = 0)
    {
        if((entry = strstr(entry, "\ndef<")) == NULL)
            break;
        entry += 5;
        count = 0;
        if(entry == strstr(entry, "int> "))
            type = 1;
        else if(entry == strstr(entry, "str> "))
            type = 2;
        entry += 5;

        if((tmp1 = strchr(entry, ',')) < (entry + strlen(entry)) && tmp1)    // 有问题，不过现在已经没有了
        {
            while(*entry != '\n')
            {
                if(*entry == ',')
                {
                    buf[i] = 0;
                    count++;
                }
                else
                {
                    buf[i] = *entry;
                }
                i++, entry++;
            }
        }
        else
        {
            while(*entry != ' ' && *entry != '\n')
            {
                buf[i] = *entry++;
                i++;
            }
        }
        buf[i] = 0;
        count++;

        tmp1 = (char *)malloc(i);
        for(int j = i; j != -1; j--)
        {
            tmp1[j] = buf[j];
        }
        if(type == 1)// 分配int相应空间
            tmp2 = malloc(count * sizeof(int));
        for(int k = 0; k != count; k++)
        {
            (*storage_ptr).var_type = type;
            if(k)
                tmp1 += (strlen(tmp1) + 1);
            (*storage_ptr).var = tmp1;
            if(type == 1)
                (*storage_ptr).val = &tmp2[k];
            else
                (*storage_ptr).val = NULL;
            storage_ptr++;
        }
    }
    for(int a = 0; storage[a].var_type; a++)
        printf("类型：%d，变量名：%s，指针：%p\n", storage[a].var_type, storage[a].var, storage[a].val);
    // End of init_stack
    // Begin of find_entry
    entry = prog + 6;
    if(strncmp(prog, "entry ", 6))
        return;
    while(*entry == ' ' && *entry)
        entry++;
    for(char *p2 = entry; *p2; p2++)
    {
        if(*p2 == ' ' || *p2 == '\n')
        {
            *p2 = 0;
            count = snprintf(buf, BUF_SIZE, "\n%s:\n", entry);
            *p2 = '\n';
            break;
        }
    }
    entry = strstr(prog, buf) + count;
    if(entry == (void *)((long)count))// 后面要做出修改
        return;
    // End of find_entry
    buf[count - 2] = 0;
    printf("程序入口：%s，地址：%p\n", &buf[1], entry);
    return;
}
